package com.aw.qzblog.service.impl;

import com.aw.qzblog.common.Result;
import com.aw.qzblog.entity.dto.user.UserLoginRequest;
import com.aw.qzblog.entity.dto.user.UserRegisterRequest;
import com.aw.qzblog.entity.enums.ErrorEnum;
import com.aw.qzblog.entity.vo.user.UserBaseInfo;
import com.aw.qzblog.entity.vo.user.UserSearchCard;
import com.aw.qzblog.exception.SysException;
import com.aw.qzblog.utils.StrUtl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.aw.qzblog.entity.model.User;
import com.aw.qzblog.service.UserService;
import com.aw.qzblog.mapper.UserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 34889
 * @description 针对表【user】的数据库操作Service实现
 * @createDate 2024-06-20 20:39:55
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    /**
     * 用户注册
     *
     * @param userRegisterRequest 用户注册实体
     * @return 成功提示
     */
    @Override
    public Result register(UserRegisterRequest userRegisterRequest) {
        if (userRegisterRequest == null) {
            throw new SysException(ErrorEnum.INPUT_ERROR);
        }
        if (!userRegisterRequest.getPassword().equals(userRegisterRequest.getCheckPassword())) {
            throw new SysException(ErrorEnum.INPUT_ERROR, "两次输入的密码不一致");
        }
        User one = lambdaQuery().eq(User::getUserAccount, userRegisterRequest.getUserAccount()).one();
        if (one != null) {
            throw new SysException(ErrorEnum.INPUT_ERROR, "该账号已存在");
        }
        User regsiterUser = new User();
        String salt = StrUtl.getRandomStr(50);
        regsiterUser.setUserAccount(userRegisterRequest.getUserAccount());
        regsiterUser.setUsername(userRegisterRequest.getUsername());
        regsiterUser.setSalt(salt);
        String password = DigestUtils.md5DigestAsHex((userRegisterRequest.getPassword() + salt).getBytes());
        regsiterUser.setPassword(password);
        this.save(regsiterUser);
        return Result.success();
    }

    /**
     * 用户登录
     *
     * @param userLoginRequest 用户登录实体
     * @return 用户信息
     */
    @Override
    public Result login(UserLoginRequest userLoginRequest) {
        if (userLoginRequest == null) {
            throw new SysException(ErrorEnum.INPUT_ERROR);
        }
        User one = lambdaQuery().eq(User::getUserAccount, userLoginRequest.getUserAccount()).one();
        if (one == null) {
            throw new SysException(ErrorEnum.INPUT_ERROR, "账号或密码错误");
        }
        UserBaseInfo userBaseInfo = new UserBaseInfo();
        BeanUtils.copyProperties(one, userBaseInfo);
        return Result.success(userBaseInfo);
    }

    /**
     * 通过用户的昵称来查询用户
     *
     * @param username 用户昵称
     * @return 用户信息
     */
    @Override
    public Result searchByName(String username) {
        List<UserSearchCard> list = lambdaQuery().like(User::getUsername, username).list().stream().map(user -> {
            UserSearchCard userSearchCard = new UserSearchCard();
            BeanUtils.copyProperties(user, userSearchCard);
            return userSearchCard;
        }).toList();
        return Result.success(list);
    }

    /**
     * 管理员分页获取用户
     *
     * @param pageNum 第几页
     * @return 用户数据
     */
    @Override
    public Result userPage(int pageNum) {
        Page<User> userPageInfo = lambdaQuery().page(new Page<>(pageNum, 20));
        Map<String, Object> userPage = new HashMap<>();
        userPage.put("total", userPageInfo.getTotal());
        userPage.put("list", userPageInfo.getRecords());
        return Result.success(userPage);
    }


    /**
     * 管理员切用户账号状态
     *
     * @param userId   用户id
     * @param statusId 切换的目标状态
     * @return 结果
     */
    @Override
    public Result changeStatus(Long userId, Integer statusId) {
        boolean update = lambdaUpdate().eq(User::getId, userId)
                .set(User::getStatus, statusId)
                .update();
        if (!update) {
            throw new SysException(ErrorEnum.SYS_ERROR, "用户状态切换失败");
        }
        return Result.success();
    }
}




